#include "stdafx.h"
#include "DataPackage.h"

#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>

#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlField>

DataPackage::DataPackage(QObject *parent)
	: QObject(parent)
{

}

DataPackage::~DataPackage()
{

}

/***********************************************************************************************************************************/
QString DataPackage::getError()
{
	return strError;
}

/***********************************************************************************************************************************/
bool DataPackage::request(QByteArray & data)
{
	//HeartBeat, 1 Need send back, 0 not need
	return request("1", JDT_HeartBeat, data);
}

bool DataPackage::request(const QString & SQL, QByteArray & data)
{
	return request(SQL, JDT_Select, data);
}

bool DataPackage::request(const QString & send, JsonDataType Type, QByteArray & data)
{
	strError.clear();
	QJsonObject object;
	object.insert("Type", Type);
	object.insert("Request", send);

	QJsonDocument doc;
	doc.setObject(object);

	if (doc.isEmpty())
	{
		strError = tr("QJsonDocument is Empty!");
		return false;
	}

	data = doc.toBinaryData();

	if (data.size() > 0)
		return true;

	strError = tr("data is null, to json failed!");
	return false;

}

/***********************************************************************************************************************************/
bool DataPackage::response(QByteArray & data)
{
	//HeartBeat, 1 Need send back, 0 not need
	return request("0", JDT_HeartBeat, data);
}

bool DataPackage::response(const QString & back, const QString & Error, QByteArray & data)
{
	strError.clear();
	QJsonObject object;
	object.insert("Type", JDT_Result);
	object.insert("Response", back);
	object.insert("Error", Error);

	QJsonDocument doc;
	doc.setObject(object);

	if (doc.isEmpty())
	{
		strError = tr("QJsonDocument is Empty!");
		return false;
	}

	data = doc.toBinaryData();

	if (data.size() > 0)
		return true;

	strError = tr("data is null, to json failed!");
	return false;

}

bool DataPackage::response(QSqlQuery & query, QByteArray & data)
{
	strError.clear();

	QJsonObject object;
	QJsonObject records;
	object.insert("Type", JDT_Records);
	
	/************************************************************************/
	//Table Data
	//Header
	QJsonArray arrFileds;
    const QSqlRecord & fields = query.record();
	int count = fields.count();
	for (int col = 0; col < count; ++col)
	{
        const QSqlField & field = fields.field(col);
		QJsonObject colObj;
        colObj.insert("name", field.name());
        colObj.insert("type", int(field.type()));
		colObj.insert("len", field.length());
		arrFileds.append(colObj);
	}
	records.insert("fields", arrFileds);

	//Datas
	QJsonArray arrDatas;
	while (query.next())
	{
		QJsonArray arrData;
		for (int i = 0; i < count; i++)
		{
			arrData.append(toJsonValue(query.value(i)));
		}

		arrDatas.append(arrData);
	}
	records.insert("records", arrDatas);

	/************************************************************************/
	object.insert("Response", records);
	QJsonDocument doc;
	doc.setObject(object);

	if (doc.isEmpty())
	{
		strError = tr("QJsonDocument is Empty!");
		return false;
	}

	data = doc.toBinaryData();

	if (data.size() > 0)
		return true;

	strError = tr("data is null, to json failed!");
	return false;
}

/***********************************************************************************************************************************/
QJsonValue DataPackage::toJsonValue(const QVariant & value)
{
	QVariant::Type type = value.type();
	switch (type)
	{
	case QVariant::Bool:
		return QJsonValue(value.toBool());
	case QVariant::Int:
		return QJsonValue(value.toInt());
	case QVariant::String:
		return QJsonValue(value.toString());

	default:
		return QJsonValue(value.toString());
	}
}
